Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
i18n made easy - full featured: middleware, template support plus clientside use
i18next is an internationalization-framework written in and for JavaScript. It provides the means to translate your web application on the client or server side. The framework comes with powerful features like pluralization, formatting, and support for multiple languages.
Basic translation
This feature allows you to translate text. You initialize i18next with a language and resources, then use the `t` function to translate a key.
i18next.init({ lng: 'en', resources: { en: { translation: { 'key': 'hello world' } } } });
console.log(i18next.t('key')); // output: hello world
Pluralization
This feature supports singular and plural forms based on a count. You define keys for singular and plural forms, and i18next chooses the correct form based on the count provided.
i18next.init({ lng: 'en', resources: { en: { translation: { 'key': 'item', 'key_plural': 'items', 'keyWithCount': '{{count}} item', 'keyWithCount_plural': '{{count}} items' } } } });
console.log(i18next.t('keyWithCount', { count: 1 })); // output: 1 item
console.log(i18next.t('keyWithCount', { count: 5 })); // output: 5 items
Contextual translation
This feature allows for translations to change based on context. You define keys with context, and i18next provides the correct translation for the given context.
i18next.init({ lng: 'en', resources: { en: { translation: { 'friend': 'A friend', 'friend_male': 'A boyfriend', 'friend_female': 'A girlfriend' } } } });
console.log(i18next.t('friend', { context: 'male' })); // output: A boyfriend
console.log(i18next.t('friend', { context: 'female' })); // output: A girlfriend
Language fallback
This feature allows you to specify fallback languages in case a translation is missing in the current language. If a key is not found in the current language, i18next will try to find it in the fallback language.
i18next.init({ lng: 'en', fallbackLng: 'fr', resources: { en: { translation: { 'key': 'hello world' } }, fr: { translation: { 'key': 'bonjour le monde' } } } });
console.log(i18next.t('key')); // output: hello world
i18next.changeLanguage('es');
console.log(i18next.t('key')); // output: bonjour le monde
Formatting
This feature allows you to include variables and formatting within your translations. You can pass variables to the translation function and format them within the translation string.
i18next.init({ lng: 'en', resources: { en: { translation: { 'key': 'The current date is {{date, MM/DD/YYYY}}' } } } });
console.log(i18next.t('key', { date: new Date() })); // output: The current date is 04/01/2023
React Intl is part of the FormatJS suite which provides internationalization support for React applications. It uses a similar approach to i18next with a focus on React components, but it is more tightly integrated with React and includes components like <FormattedMessage> for rendering translated messages.
Polyglot.js is a tiny I18n helper library that provides simple translation support with variable interpolation. It is smaller and simpler than i18next, but it lacks some of the more advanced features like pluralization and context handling.
Vue I18n is an internationalization plugin for Vue.js. It is very similar to i18next but is designed specifically for Vue.js applications. It provides many of the same features but integrates more seamlessly with Vue's reactivity system.
Globalize is a library for internationalization and localization that leverages the official Unicode CLDR JSON data. It is more focused on providing accurate number formatting, date and time formatting, and message formatting across cultures, rather than just translation.
Currently i work hard on getting out v2.0.0 of i18next. It's a complete rewrite of the current code base and will assert i18next is ready for the next things that will come.
Current state is still alpha - but i highly encourage maintainers of modules using i18next - to take the early chance to play with the upcoming version and starting to migrate to v2.
For people using i18next in production i recommend waiting for first beta version to play around.
more info could be found on: Migration Guide
Project goal is to provide the same easy way to translate a website on serverside like in i18next on the clientside:
npm install i18next
First require the module and init i18next:
var i18next = require('i18next');
i18next.init(); // for options see i18next-node gh-page
Register the express middleware, so we can check current language settings:
// Configuration
app.configure(function() {
app.use(express.bodyParser());
app.use(i18next.handle);
app.use(app.router);
[...]
});
Register AppHelper so you can use the translate function in your template:
i18next.registerAppHelper(app)
Now you can (depending on your template language) do something like this in your template(jade example):
body
span= t('app.name')
To serve the clientside script and needed routes for resources and missing keys:
i18next.serveClientScript(app)
.serveDynamicResources(app)
.serveMissingKeyRoute(app);
now you can add the script to you page and use i18next on the client like on the server:
script(src='i18next/i18next.js', type='text/javascript')
$.i18n.init([options], function() {
$('#appname').text($.t('app.name'));
});
for more information on clientside usage have a look at i18next
Copyright (c) 2014 Jan Mühlemann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
i18next internationalization framework
The npm package i18next receives a total of 5,969,630 weekly downloads. As such, i18next popularity was classified as popular.
We found that i18next demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.